package DEEPERsource.DEEPERsource.source.statistics;

import edu.uci.ics.jung.graph.DirectedEdge;
import edu.uci.ics.jung.graph.Edge;
import edu.uci.ics.jung.graph.Vertex;
import graph.Keys;
import jung.entities.NewPath;
import jung.entities.Path;

public class DepTreeAnalyzer {

	public FreqMap<String> wordFrequency(NewPath dep){
		FreqMap<String> map = new FreqMap<String>();
		for(Vertex v : dep.vertices){
			map.put((String)v.getUserDatum(Keys.LABEL),1);
		}
		return map;
	}
	
	public FreqMap<String> depFrequency(NewPath dep){
		FreqMap<String> map = new FreqMap<String>();
		for(Edge v : dep.edges){
			map.put((String)v.getUserDatum(Keys.LABEL),1);
		}
		return map;
	}
	
	public FreqMap<String> depFrequencyN(NewPath dep, int n){
		FreqMap<String> map = new FreqMap<String>();
		for(DirectedEdge e : dep.edges){
			String key = obtainKey(dep, n, e);
			map.put(key,1);
		}
		return map;
	}
	
	private String obtainKey(NewPath dep, int n, DirectedEdge e){
		String key = (String)e.getUserDatum(Keys.LABEL);
		for(int i = n-1; i>0; i--){
			if(e.getDest().getOutEdges().size()==0){
				return "none";
			}else{
				e = (DirectedEdge)e.getDest().getOutEdges().iterator().next();
				key = key + "~" + (String)e.getUserDatum(Keys.LABEL);
			}
		}
		return key;
	}
	
	public String[] root(NewPath dep){
		Vertex[] roots = dep.getRoots();
		if(roots.length>0){		
			String[] result = new String[roots.length];
			int i = 0;
			for(Vertex root : roots){
				result[i++] = (String)root.getUserDatum(Keys.LABEL);
			}
			return result;
		}else{
			return null;
		}
	}
}
